package com.keyroy.game.puzzle_rpg.views.listener;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public abstract class ListTableListener implements TableListener {
	public static final String LIST_TAG = "list";

	@Override
	public void onSelected(Table table, final TableEditor tableEditor, final TableItem tableItem, final int editIndex) {
		final String[] strings = (String[]) tableItem.getData(LIST_TAG);
		final Combo combo = new Combo(table, SWT.READ_ONLY);
		combo.setBounds(51, 387, 297, 25);

		combo.setItems(strings);
		combo.select(0);

		combo.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e) {
				String text = strings[combo.getSelectionIndex()];
				if (onValueChange(strings[combo.getSelectionIndex()])) {
					TableItem tableItem = tableEditor.getItem();
					tableItem.setText(editIndex, text);
				}
				combo.dispose();
			}
		});
		tableEditor.setEditor(combo, tableItem, editIndex);
		combo.forceFocus();
	}

	public abstract boolean onValueChange(String text);

}
